﻿@using Newtouch.Infrastructure;
@{
    ViewBag.Title = "TradeInfo";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<style>
    .ui-jqgrid tr.jqgrow td {
        white-space: normal !important;
        height:auto;
        vertical-align:text-top;
        padding-top:2px;
    }
</style>

<from id="form1">
    <div>
        <table class="form">
            <tr>
                <th class="formTitle">订单号：</th>
                <td class="formValue">
                    <input type="text" class="form-control" style="border: none;" readonly="readonly" id="OutTradeNo"/>
                </td>
                <th class="formTitle">交易状态：</th>
                <td class="formValue">
                    <input type="text" class="form-control" style="border: none;" readonly="readonly" id="TradeStatus"/>
                </td>
                <th class="formTitle" ></th>
                <td class="formValue"  rowspan="2">
                    <input type="button" class="btn btn-default btn-md" style="width:100px; " value="刷新" id="btn_refresh" onclick="traderefresh()"  />
                </td>
            </tr>
            <tr>
                <th class="formTitle">总金额：</th>
                <td class="formValue">
                    <input type="text" class="form-control" style="border: none;" readonly="readonly" id="TotalAmount" />
                </td>
                <th class="formTitle">交易时间：</th>
                <td class="formValue">
                    <input type="text" class="form-control" style="border: none;" readonly="readonly" id="SendPayDate"/>
                </td>
            </tr>
            <tr>
                <th class="formTitle">已退金额：</th>
                <td class="formValue">
                    <input type="text" class="form-control" style="border: none;" readonly="readonly" id="ytje" />
                </td>
                <th class="formTitle"></th>
                <td class="formValue"></td>
                <th class="formTitle"></th>
                <td class="formValue">
                    <input type="button" class="btn btn-default btn-md" style="width:100px; " value="申请退款" id="btn_refund" onclick="tradeRefund()"  />
                </td>
            </tr>
            <tr>
                <th class="formTitle">剩余可退：</th>
                <td class="formValue">
                    <input type="text" class="form-control" style="border: none;" readonly="readonly" id="maxrfd" />
                </td>
            </tr>
        </table>
    </div>
    <div style="padding-left:5px;padding-right:5px;">
        <table id="gridListRefd"></table>
        @*<div id="gridRefdPager"></div>*@
    </div>

</from>
<script>
    var keyValue = $.request('outtradeno');
    var maxrfd = 0.00;
    $(function() {
        RefundGrid();
        $.najax({
            type: "GET",
            url: "/PayManage/MicroPay/GetTradeInfo?outTradeNo=" + keyValue,
            loading: true,
            loadingtext: "正在查询下最新交易信息，请稍后...",
            success: function (ajaxresp) {
                maxrfd = (parseFloat(ajaxresp.PayAmount) - parseFloat(ajaxresp.RefundAmount)).toFixed(2);
                if (ajaxresp != null && ajaxresp != undefined) {
                    $("#OutTradeNo").val(ajaxresp.OutTradeNo);
                    $("#TotalAmount").val(ajaxresp.PayAmount +" (元)");
                    $("#SendPayDate").val(ajaxresp.OrderDate.replace("T"," "));
                    $("#ytje").val(ajaxresp.RefundAmount.toFixed(2) + " (元)");
                    $("#maxrfd").val(maxrfd + " (元)");

                    $("#TradeStatus").val(ajaxresp.TradeStatus);
                    var stu = "";
                    if (ajaxresp.TradeStatus == "TRADE_SUCCESS") {
                        stu = "交易成功";
                    }
                    else if (ajaxresp.TradeStatus == "TRADE_FINISHED") {
                        stu = "交易完成";
                    }
                    else if (ajaxresp.TradeStatus == "TRADE_CLOSED") {
                        stu = "交易关闭";
                    } else {
                        stu = "交易进行中";
                    }
                    $("#TradeStatus").val(stu);
                    if (maxrfd > 0 && ajaxresp.TradeStatus != "TRADE_CLOSED") {

                        $("#btn_refund").show();
                    } else {
                        $("#btn_refund").hide();
                    }
                };
            }
        });
    });



    function RefundGrid() {
        var $gridJsList = $("#gridListRefd");
        $gridJsList.dataGrid({
            url: '/PayManage/MicroPay/TradeRefundList',
            height: 200,
            postData: { outTradeNo: keyValue, tradeNo: "" },
            colModel: [
                //{ label: '支付方式', name: 'PayType', width: 60, align: 'left' },
                {
                    label: '订单号',
                    name: 'OutTradeNo',
                    width: 100,
                    align: "left"
                },
                {
                    label: '退款状态',
                    name: 'RefundStatus',
                    width: 60,
                    align: 'left',
                    formatter: function(cellvalue) {
                        return $.enum.getDescByValue("EnumRefundStatus", cellvalue);
                    }
                },
                { label: '退款说明', name: 'RefundReason', width: 60, align: 'left' },
                {
                    label: '退款金额',
                    name: 'Amount',
                    width: 80,
                    align: 'left',
                    formatter: function(cellvalue) {
                        return cellvalue ? cellvalue.toFixed(2) : "0.00";
                    }
                },
                { label: '备注', name: 'Memo', width: 160, align: 'left' },
                {
                    label: '退款时间',
                    name: 'RefundDate',
                    width: 150,
                    formatter: "date",
                    formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' }
                },
                {
                    label: '退款请求号',
                    name: 'OutRequestNo',
                    hidden: true,
                    align: "left"
                },
                {
                    label: '操作',
                    name: 'Action',
                    width: 60,
                    align: "center",
                    formatter: function (cellvalue, option, row) {
                        
                        if (row.RefundStatus == "@((int) EnumRefundStatus.UnKnown)") {
                            return "<a onclick='refundRetry(" + option.rowId + ")'>重试</a>";
                        } else {
                            return "";
                        }

                    }
                }
            ],
            //pager: "#gridRefdPager",
            sortname: "OutRequestNo",
            viewrecords: true,
            caption: "退费记录"
        });
    }

    function tradeRefund() {
        var total = $("#TotalAmount").val();
        $.modalOpen({
            id: "FormRfd",
            title: "交易详情",
            url: "/PayManage/MicroPay/TradeRefund?outtradeno=" + keyValue + "&maxrfd=" + maxrfd + "&total=" + total,
            width: "300px",
            height: "200px",
            callBack: function(iframeId) {
                top.frames[iframeId].submitForm(function() {
                    setTimeout("window.location.reload()", 100);
                    //window.location.reload();
                }); //窗口点确定的回调函数

            }
        });


    }

    function refundRetry(rowId) {
        var row = $('#gridListRefd').jqGrid("getRowData", rowId);
        $.najax({
            type: "GET",
            url: "/PayManage/MicroPay/Opr_TradeRefund?outTradeNo=" + row.OutTradeNo + "&refundAmount=" + row.Amount + "&Reason=未知退款重试&OutRequestNo=" + row.OutRequestNo,
            loading: true,
            loadingtext: "申请退费中，请稍后...",
            success: function (ajaxresp) {
            }
        });
    }

    function traderefresh() {
        window.location.reload();
    }
</script>